﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using System.Web;
using YouHP.Model.Group;
using YouHP.Pages;

namespace web.Pages.xml
{
    public partial class HistoryWeightAdd : XmlBasePage
    {
        protected int _iOperationType; //0日期有重复，不修改，1强制修改
        WeightHistoryInfo _info;
       
        override protected RET_CODE CheckParam(ref string sMsg)
        {
            _iOperationType = RequestTools.GetInt("type", 0);

            _info = new WeightHistoryInfo();
            double weight = RequestTools.GetFormDouble("wvalue", -1);
            if (weight <= 0)
            {
                sMsg = "请填写体重信息";
                return RET_CODE.RET_INPUT_NOWEIGHT;
            }

            //如果录入身高信息
            _info.Height = 0;
            int hasHeight = RequestTools.GetFormInt("hasheight", -1);
            if (hasHeight > 0)
            {
                double tmpHeight = RequestTools.GetFormDouble("height", -1);
                if (tmpHeight < 0 || tmpHeight > 3)
                {
                    sMsg = "请填写正确的身高信息";
                    return RET_CODE.RET_NOHEIGHT;
                }
                _info.Height = tmpHeight;
            }
           
            // 对时间进行判断
            string va = RequestTools.GetString("odate");
            if (va == "")
            {
                sMsg = "请填写时间";
                return RET_CODE.RET_INPUT_NOTIME;
            }            
            
            DateTime dt;
            bool succ = DateTime.TryParse(va, out dt);
            if (succ == false)
            {
                sMsg = "错误的时间 " + va;
                return RET_CODE.RET_INPUT_ERRTIME;
            }

            if (DateTime.Compare(dt, DateTime.Today) > 0)
            {
                sMsg = "记录日期 " + dt.ToString("yyyy-MM-dd") + " 不能比当前日期 " + DateTime.Today.ToString("yyyy-MM-dd") + " 晚！";
                return RET_CODE.RET_INPUT_ERRTIME;
            }
            else if (DateTime.Compare(dt, PrData.StartDate) < 0)
            {
                sMsg = "记录日期 " + dt.ToString("yyyy-MM-dd") + " 不能比第一次发病日期 " + PrData.StartDate.ToString("yyyy-MM-dd") + " 早！";
                return RET_CODE.RET_INPUT_INVALIDTIME;
            }

             _info.RecordDate = dt;
            _info.DataValue = weight;
            _info.PRId = PrData.Id;
            _info.UpdateDate = DateTime.Now;  
            
            // 检查时间是否重复
            if (PatientBLL.Instance.CheckWeightHistory(PrData.Id, dt))
            {
                //提示是否需要覆盖历史记录
                sMsg = "是否需要覆盖" + va + "已有的历史记录？";
                return RET_CODE.RET_INPUT_SAMERECORD;
            }

            return RET_CODE.RET_SUC;
        }

        override protected void Handle()
        {
            if (!CheckLogin())
            {
                ResponseContext(RET_CODE.RET_NOLOGIN, "你还没有登录");
                return;
            }
            PrData = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);

            if (PrData.Id <= 0)
            {
                ResponseContext(RET_CODE.RET_NORECORD, "请先填写患病基本信息");
                return;
            }

            string sErrMsg = "";
            RET_CODE iRet = CheckParam(ref sErrMsg); 

            //如果不成功，或者还有记录，但是重复
            if (RET_CODE.RET_SUC != iRet && !(RET_CODE.RET_INPUT_SAMERECORD == iRet && 1 == _iOperationType))
            {
                ResponseContext(iRet, sErrMsg);
                return;
            }

            bool succ = false;
            if (_info.Height > 0)
            {
                succ = PatientBLL.Instance.UpdatePatientHeight(UserData.Id, _info.Height);
                if (!succ)
                {
                    ResponseContext(RET_CODE.RET_NOHEIGHT, "修改身高信息失败");
                    return;
                }
            }           
            
            succ = PatientBLL.Instance.AddWeightHistory(_info);
            if (succ == true)
            {
                ResponseContext(RET_CODE.RET_SUC, "成功");
                return;
            }
            else
            {
                ResponseContext(RET_CODE.RET_ERROR, "添加体重信息失败");
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            Handle();
        }
    }
}